package cn.tedu.ivos2.geofence.service.Impl;

import cn.tedu.ivos2.base.exception.ServiceException;
import cn.tedu.ivos2.base.response.StatusCode;
import cn.tedu.ivos2.geofence.mapper.GeofenceMapper;
import cn.tedu.ivos2.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivos2.geofence.pojo.dto.GeofenceSaveDTO;
import cn.tedu.ivos2.geofence.pojo.entity.Geofence;
import cn.tedu.ivos2.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivos2.geofence.service.GeofenceService;
import cn.tedu.ivos2.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos2.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos2.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos2.vehicle.pojo.vo.VehicleVO;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> select(GeofenceQuery geofenceQuery) {
        log.debug("查询围栏:{}",geofenceQuery);
        List<GeofenceVO> geofenceVOList = geofenceMapper.query(geofenceQuery);
        for (int i=0;i<geofenceVOList.size();i++){
            GeofenceVO geofenceVO = geofenceVOList.get(i);
            //获取该围栏下的所有车辆
            VehicleQuery vehicleQuery=new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOList = vehicleMapper.query(vehicleQuery);
            geofenceVO.setTotalNum(vehicleVOList.size());
            //获取围栏下的可用车辆
            int availableNum=0;
            for (VehicleVO v:vehicleVOList){
                if ("1".equals(v.getStatus())){
                    availableNum++;
                }
            }
            if (availableNum>0){
                geofenceVO.setAvailableNum(availableNum);
            }else {
                geofenceVO.setAvailableNum(0);
            }
            //把查询的车辆结果一并返回了
            geofenceVO.setVehicleList(vehicleVOList);
        }

        return geofenceVOList;
    }

    @Override
    public void updateStatus(Long id, String status) {
        Geofence geofence=new Geofence();
        geofence.setId(id);
        geofence.setStatus(status);
        geofenceMapper.update(geofence);
    }

    @Override
    public void delete(Long id) {
        VehicleQuery vehicleQuery=new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> query = vehicleMapper.query(vehicleQuery);
        log.debug("当前围栏绑定车辆:{}",query);
        if (query.size()==0){
            geofenceMapper.deleteById(id);
        }else {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }

    }

    @Override
    public void inset(GeofenceSaveDTO geofenceSaveDTO) {
        Geofence geofence=new Geofence();
        BeanUtils.copyProperties(geofenceSaveDTO,geofence);

        //前端传过来键值对形式的数据,需要处理
        HashMap<String,String>position=geofenceSaveDTO.getPosition();
        ObjectMapper objectMapper=new ObjectMapper();
        try {
            if (geofence.getId()!=null){
                geofenceMapper.update(geofence);
            }else {
                String positionStr=objectMapper.writeValueAsString(position);
                geofence.setStatus("1");
                geofence.setCreateTime(new Date());
                geofence.setPosition(positionStr);
                geofenceMapper.insert(geofence);
            }
        } catch (JsonProcessingException e){
            throw new RuntimeException(e);
        }

    }
}
